/**
 * 数据标准化初始化
 */
var StandDic = {
    id: "StandDicTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    parentId:0
};
StandDic.refresh =function(){
    StandDic.table.refresh();
    StandDic.ztree.init();
};
/**
 * 初始化表格的列
 */
StandDic.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '字典名称', field: 'name', align: 'center', width: '200px', valign: 'middle', sortable: true},
        {title: '备注', field: 'resume', align: 'center', width: '250px',valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '80px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:StandDic.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                _html += '<a href="javascript:StandDic.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="hisui-icon icon-cancel"></i></a>';
                return _html;
            }}];
};

/**
 * 检查是否选中
 */
StandDic.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        StandDic.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加标准字典
 */
StandDic.add = function () {
    this.layerIndex = Sys.open('新增标准字典',Sys.backendPath + '/sds/sdsstanddic/to_add/'+StandDic.parentId, ['800px', '480px']);
};

/**
 * 对照数据源
 */
StandDic.compareDataSource = function () {
    var index = Sys.open('对照标化字典',Sys.backendPath + '/sds/sdsstanddatacontrast/');
    this.layerIndex = index;
};

/**
 * 打开查看标准字典详情
 */
StandDic.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        this.layerIndex = Sys.open('修改标准字典',Sys.backendPath + '/sds/sdsstanddic/to_update/' + id, ['800px', '480px']);
    }
};

/**
 * 删除部门
 */
StandDic.del = function (id,name) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var haveChildren = false;

        var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddic/isHaveChildren", function (data) {
            if (data) {
                haveChildren = true
            }
        }, function (data) {

        });
        ajax.set("sdsStandDicId", id);
        ajax.start();

        if (haveChildren){
            var operation = function(){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddic/deleteAll", function (data) {
                    if (data.code==200) {
                        Sys.success("删除成功!");
                        StandDic.refresh();
                    } else {
                        Sys.error("删除失败!" + data.message + "!");
                    }
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdsStandDicId", id);
                ajax.start();
            };
            Sys.confirm("该字典含有子节点，是否一起删除?",operation);
        }else {
            var operation = function(){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddic/delete", function (data) {
                    if (data.code==200) {
                        Sys.success("删除成功!");
                        StandDic.refresh();
                    } else {
                        Sys.error("删除失败!" + data.message + "!");
                    }
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdsStandDicId", id);
                ajax.start();
            };
            Sys.confirm("是否删除字典【" + name + "】?",operation);
        }
    }
};

/**
 * 查询部门列表
 */
StandDic.search = function () {
    var queryData = {};
    queryData['parentId'] = StandDic.parentId;
    queryData['name'] = $("#condition").val();
    StandDic.table.refresh({query: queryData});
};

StandDic.onClickDic = function (e, treeId, treeNode) {
    StandDic.parentId = treeNode.id;
    StandDic.search();
};

/**
 * 打开查看项目标准化字典对照参数维护
 */
StandDic.compareParam = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        this.layerIndex = Sys.open('标准化字典对照参数维护',Sys.backendPath + '/sds/sdsstanddic/to_showDicMatch/' + id, ['800px', '480px']);
    }
};

StandDic.impParam = function () {
    var index = Sys.open('导入标准化字典对照参数', Sys.backendPath + '/sds/sdsstanddic/to_import', ['550px', '300px']);
    this.layerIndex = index;
};

StandDic.template = function () {
    window.location.href = Sys.backendPath+"/static/backend/sds/sdsstanddic/tmp/DicMatchParam_template.xlsx";
};

$(function () {
    var ztree = new $ZTree("dicTree", Sys.backendPath + "/sds/sdsstanddic/tree");
    ztree.bindOnClick(StandDic.onClickDic);
    ztree.init();
    StandDic.ztree = ztree;
    fuzzySearch('dicTree','#seachKey',null,true);

    var defaultColunms = StandDic.initColumn();
    var table = new BSTable(StandDic.id, Sys.backendPath + "/sds/sdsstanddic/list", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-10);
    table.setQueryParams({"parentId":0});
    table.setPageSize(10);
    //table.setSort("weight");
    table.init();
    StandDic.table = table;

});
